/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mavenrentcars.service.impl;

import com.mavenrentcars.dao.RatingDao;
import com.mavenrentcars.exception.ViewException;
import com.mavenrentcars.model.Car;
import com.mavenrentcars.model.Rating;
import com.mavenrentcars.model.User;
import com.mavenrentcars.service.CarService;
import com.mavenrentcars.service.RatingService;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 *
 * @author student
 */
@Service("ratingService")
public class RatingServiceImpl implements RatingService {

    @Resource(name = "ratingDao")
    RatingDao ratingDao;
    @Resource(name = "carService")
    CarService carService;

    @Override
    public boolean isVote(Car car, User user) {
        boolean vote = true;
        Rating rating = ratingDao.get(car, user);
        if (rating == null) {
            vote = false;
        }
        return vote;
    }

    @Override
    public Rating set(Car car, User user, int val) throws ViewException {
        Rating model = new Rating();
        model.setCar(car);
        model.setUser(user);
        Rating res = ratingDao.save(model);

        car.setRating((car.getRating() + val));
        carService.save(car);
        return res;
    }

    @Override
    public Rating save(Rating model) throws ViewException {
        return ratingDao.save(model);
    }

    @Override
    public Rating get(Integer id) throws ViewException {
        return ratingDao.get(id);
    }

    @Override
    public void remove(Integer id) throws ViewException {
        ratingDao.remove(id);
    }

    @Override
    public List<Rating> getAll() throws ViewException {
        return ratingDao.getAll();
    }
}
